﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DefCore.Tools;
using DefCore.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krypton.Lights;
using Krypton;
using DefCore.Interfaces;
using DefCore.Structures;
using Ivory;
using FarseerPhysics.Dynamics;
using FarseerPhysics.SamplesFramework;

namespace DefCore.Core
{
    public abstract class EntityBase : ISelectable, IViewable, ITickListener
    {
        //Fields
        public LevelBase ParentLevel;
        
        public DungeonPos Position
        {
            get;
            set;
        }
        public Vector2 Size
        {
            get;
            set;
        }

        public abstract Light2D EntityLight { get; }
        public abstract ShadowHull EntityHull { get; }

        public Body EntityBody
        {
            get;
            protected set;
        }
        public virtual Rectangle HitBox
        {
            get
            {
                return new Rectangle(
                    (int)(Position.ScreenSpaceCoords.X),
                    (int)(Position.ScreenSpaceCoords.Y),
                    (int)Size.X, (int)Size.Y);


                //
                //if (CellBody == null)
                //    return new Rectangle(
                //        (int)(MapPosition.WorldPosition.X - ObjectHolder.GameCamera.CameraTopLeft.X),
                //        (int)(MapPosition.WorldPosition.Y - ObjectHolder.GameCamera.CameraTopLeft.Y),
                //        (int)Globals.TileSize.X, (int)Globals.TileSize.Y);
                //else
                //{

                //    Rectangle r = new Rectangle(
                //        (int)(ConvertUnits.ToDisplayUnits(CellBody.Position.X) - (Globals.TileSize.X / 2f)),
                //        (int)(ConvertUnits.ToDisplayUnits(CellBody.Position.Y) - (Globals.TileSize.Y / 2f)),
                //        (int)Globals.TileSize.X, (int)Globals.TileSize.Y);

                //    r.X -= (int)(ObjectHolder.GameCamera.CameraTopLeft.X);
                //    r.Y -= (int)(ObjectHolder.GameCamera.CameraTopLeft.Y);
                //    return r;
                //}
            }
        } 
        
        public virtual int HitIndex
        {
            get { return 1; }
        }
        public virtual bool IsSelected
        {
            get;
            set;
        }
        public virtual bool IsHoverTarget
        {
            get;
            set;
        }
        public virtual bool IsSelectable
        {
            get;
            protected set;
        }
        
        //ctor
        public EntityBase(LevelBase parent)
        {
            this.ParentLevel = parent;
            CreateBody();
        }

        //Methods
        public virtual void Create()
        {
            SelectionEngine.Instance.Selectables.Add(this);
            TickControl.Instance.OnTick += this.OnGlobalTick;
        }
        public virtual void Delete()
        {
            if (EntityBody != null)
            {
                Objects.GameWorld.RemoveBody(this.EntityBody);
                this.EntityBody = null;
            }

            SelectionEngine.Instance.Selectables.Remove(this);
            TickControl.Instance.OnTick -= this.OnGlobalTick;
        }

        protected virtual void CreateBody()
        {
            
        }

        public virtual void OnGlobalTick(object sender, GlobalTickEventArgs e) { }
        public virtual bool Load() { return true; }  

        public abstract void Render(SpriteBatch batch, GameTime time);
        public abstract void Solve(InputControl control, GameTime time);              
    }
}
